package com.hys.app.converter.erp;

import com.hys.app.framework.database.WebPage;
import com.hys.app.model.base.context.Region;
import com.hys.app.model.erp.dos.OrderDO;
import com.hys.app.model.erp.dos.OrderItemDO;
import com.hys.app.model.erp.dos.OrderPaymentDO;
import com.hys.app.model.erp.dos.StoreDO;
import com.hys.app.model.erp.dto.OrderDTO;
import com.hys.app.model.erp.enums.OrderSourceEnum;
import com.hys.app.model.erp.enums.OrderStatusEnum;
import com.hys.app.model.erp.enums.OrderTypeEnum;
import com.hys.app.model.erp.vo.OrderExcelVO;
import com.hys.app.model.erp.vo.OrderItemVO;
import com.hys.app.model.erp.vo.OrderVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 订单 Converter
 *
 * @author 张崧
 * 2024-01-24 15:58:31
 */
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface OrderConverter {

    OrderDO convert(OrderDTO orderDTO);

    @Mapping(target = "allowable", expression = "java(new com.hys.app.model.erp.vo.OrderAllowable(orderDO))")
    @Mapping(target = "statusText", expression = "java(orderDO.getStatus().getText())")
    @Mapping(target = "deliveryTypeText", expression = "java(orderDO.getDeliveryType().getText())")
    @Mapping(target = "paymentStatusText", expression = "java(orderDO.getPaymentStatus().getText())")
    @Mapping(target = "sourceText", expression = "java(orderDO.getSource().getText())")
    OrderVO convert(OrderDO orderDO);

    List<OrderVO> convertList(List<OrderDO> list);

    List<OrderExcelVO> convertExcel(List<OrderVO> list);

    WebPage<OrderVO> convertPage(WebPage<OrderDO> webPage);

    default OrderDO combination(OrderDTO orderDTO) {
        boolean isToC = orderDTO.getType() == OrderTypeEnum.TO_C;
        boolean isInternal = orderDTO.getSource() == OrderSourceEnum.Internal;
        OrderDO orderDO = convert(orderDTO);
        if (isToC) {
            // 会员
            orderDO.setOwnerName(orderDTO.getMemberDO().getName());
            orderDO.setOwnerMobile(orderDTO.getMemberDO().getMobile());
        } else {
            if (isInternal) {
                // 企业
                orderDO.setOwnerName(orderDTO.getEnterpriseDO().getName());
                orderDO.setOwnerMobile(orderDTO.getEnterpriseDO().getMobile());
            } else {
                // 第三方用户
                orderDO.setOwnerId(orderDTO.getOwnerId());
                orderDO.setOwnerName(orderDTO.getThirdOwnerName());
                orderDO.setOwnerMobile(orderDTO.getThirdOwnerMobile());
            }
        }
        // 仓库
        orderDO.setWarehouseName(orderDTO.getWarehouseDO() == null ? "" : orderDTO.getWarehouseDO().getName());
        // 部门
        if (orderDTO.getDeptDO() != null) {
            orderDO.setDeptId(orderDTO.getDeptDO().getId());
            orderDO.setDeptName(orderDTO.getDeptDO().getName());
        }
        // 销售经理
        orderDO.setMarketingName(orderDTO.getMarketingManagerDO() == null ? "" : orderDTO.getMarketingManagerDO().getRealName());
        // 配送信息
        Region region = orderDTO.getRegion();
        if (region != null) {
            orderDO.setShipRegionIds(Arrays.asList(region.getProvinceId(), region.getCityId(), region.getCountyId(), region.getTownId()));
            orderDO.setShipRegionNames(Arrays.asList(region.getProvince(), region.getCity(), region.getCounty(), region.getTown()));
        }
        orderDO.setStoreName(orderDTO.getStoreDO() == null ? "" : orderDTO.getStoreDO().getStoreName());
        orderDO.setWarehouseOutFlag(false);
        orderDO.setShipFlag(false);

        return orderDO;
    }

    default OrderVO convert(OrderDO orderDO, List<OrderItemDO> itemList, List<OrderPaymentDO> paymentList, StoreDO storeDO, Map<Long, Integer> stockNumMap) {
        OrderVO orderVO = convert(orderDO);

        // 订单明细
        List<OrderItemVO> orderItemList = convertItemList(itemList);
        orderItemList.forEach(orderItemVO -> {
            if (orderDO.getType() == OrderTypeEnum.TO_B) {
                orderItemVO.setStockNum(stockNumMap.get(orderItemVO.getProductId()));
            } else {
                orderItemVO.setStockNum(stockNumMap.get(orderItemVO.getBatchId()));
            }
        });
        orderVO.setItemList(orderItemList);
        // 支付列表
        orderVO.setPaymentList(paymentList);
        // 门店信息
        orderVO.setStore(storeDO);

        return orderVO;
    }

    List<OrderItemVO> convertItemList(List<OrderItemDO> itemList);

    default OrderStatusEnum convertAuditStatus(Boolean isPass, OrderTypeEnum type) {
        if (isPass) {
            return type == OrderTypeEnum.TO_C ? OrderStatusEnum.COMPLETE : OrderStatusEnum.WAIT_WAREHOUSE_OUT;
        } else {
            return OrderStatusEnum.AUDIT_REJECT;
        }
    }
}

